package com.shanghaionstar.user.service;

import android.content.Context;
import android.graphics.Bitmap;

import com.onstar.cn.api.PublicService;
import com.onstar.cn.common.util.Base64;
import com.onstar.cn.common.util.Constants;
import com.onstar.cn.common.util.DeviceUtil;

import com.onstar.cn.framework.annotation.http.HeadsValue;
import com.onstar.cn.framework.annotation.http.Request;
import com.onstar.cn.framework.mvp.base.core.BaseService;
import com.onstar.cn.framework.network.HttpInjectUtils;
import com.onstar.cn.framework.network.entity.RequestEntity;
import com.onstar.cn.framework.network.enumtype.HttpAccept;
import com.onstar.cn.framework.network.enumtype.HttpMethod;
import com.onstar.cn.framework.network.enumtype.Language;
import com.onstar.cn.framework.utils.GsonHelper;
import com.onstar.cn.more.entity.FeedbackReq;
import com.onstar.cn.weight.DeleteEditText;
import com.shanghaionstar.user.FeedBackResult;
import com.shanghaionstar.utils.ApplicationUtil;
import com.shanghaionstar.utils.BusinessUtil;
import com.shanghaionstar.utils.NetConnectUtil;

import java.io.ByteArrayOutputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Mengzhou Gu on 2016/8/8.
 */
public class FeedBackService extends BaseService<FeedBackResult> {
    private static final int TIME_OUT = 60*1000;
    private List<ImgItem> imgList = new ArrayList<>();
    private DeleteEditText feedback_content;
    private  Context        context;
    public FeedBackService(Context context, List<ImgItem> imgList, DeleteEditText feedback_content){
        this.context=context;
        this.imgList=imgList;
        this.feedback_content=feedback_content;
    }
    @Override
    @Request(heads = @HeadsValue(accept = HttpAccept.JSON, language = Language.AUTO), method = HttpMethod.PUT)
    public RequestEntity getRequestEntity() {
        PublicService service = new PublicService(Constants.CURRENT_SERVER_URL);
        RequestEntity requestEntity = NetConnectUtil.getAuthRequestEntity(context);
        service.saveFeedback(requestEntity);
        FeedbackReq feedbackReq = new FeedbackReq();
        String appVersion = new DeviceUtil(context).getAppVersion();
        feedbackReq.setAppVersion(appVersion);
        String vin = ApplicationUtil.getInstance().getVin();
        feedbackReq.setVin(vin == null ? "" : vin);
        String accountID = ApplicationUtil.getInstance().getAccountId();
        feedbackReq.setAccountID(accountID == null ? "" : accountID);
        String ipdId = ApplicationUtil.getInstance().getIdpID();
        feedbackReq.setIdpID(ipdId == null ? "" : ipdId);
        String email = ApplicationUtil.getInstance().getEmail();
        feedbackReq.setEmail(email == null ? "" : email);
        String mobile = ApplicationUtil.getInstance().getMobile();
        feedbackReq.setMobile(mobile == null ? "" : mobile);
        String comment = feedback_content.getText().toString();
        feedbackReq.setComments(comment == null ? "" : comment);
        List<FeedbackReq.AattachedImg> imgs = new ArrayList<>();
        for (int i = 0; i < imgList.size() - 1; i++) {
            ImgItem item = imgList.get(i);
            FeedbackReq.AattachedImg img = new FeedbackReq.AattachedImg();
            img.setName(item.extension);
            byte[] bytes = bitmap2Bytes(item.bitmap);
            String bitmapBytes = Base64.encode(bytes);
            img.setContent(bitmapBytes);
            imgs.add(img);
        }
        feedbackReq.setBase64AttachedImageList(imgs);
        requestEntity.setBody(GsonHelper.toJson(feedbackReq));
        requestEntity.setTimeout(TIME_OUT);
        requestEntity = HttpInjectUtils.injectHttp(FeedBackService.this.getClass(), "getRequestEntity", requestEntity);
        return requestEntity;
    }

    @Override
    public FeedBackResult jsonToBean(String json) {
        return GsonHelper.fromJson(json,FeedBackResult.class);
    }
/*
    @Request(heads = @HeadsValue(accept = HttpAccept.JSON, language = Language.AUTO), method = HttpMethod.PUT)
    public RequestEntity uploadFeedbackImgs(){
        PublicService service = new PublicService(Constants.CURRENT_SERVER_URL);
        RequestEntity requestEntity = NetConnectUtil.getAuthRequestEntity(context);;
        LinkedHashMap<String, Object> coHashMap = new LinkedHashMap<String, Object>();
        coHashMap.put(FinalConstants.HTTP_AUTHORIZATION, ApplicationUtil.getInstance().getToken());
        requestEntity.setCustomHeads(coHashMap);
        service.saveFeedback(requestEntity);
        FeedbackReq feedbackReq = new FeedbackReq();
        String appVersion = new DeviceUtil(context).getAppVersion();
        feedbackReq.setAppVersion(appVersion);
        String vin = ApplicationUtil.getInstance().getVin();
        feedbackReq.setVin(vin == null ? "" : vin);
        String accountID = ApplicationUtil.getInstance().getAccountId();
        feedbackReq.setAccountID(accountID == null ? "" : accountID);
        String ipdId = ApplicationUtil.getInstance().getIdpID();
        feedbackReq.setIdpID(ipdId == null ? "" : ipdId);
        String email = ApplicationUtil.getInstance().getEmail();
        feedbackReq.setEmail(email == null ? "" : email);
        String mobile = ApplicationUtil.getInstance().getMobile();
        feedbackReq.setMobile(mobile == null ? "" : mobile);
        String comment = feedback_content.getText().toString();
        feedbackReq.setComments(comment == null ? "" : comment);
        List<FeedbackReq.AattachedImg> imgs = new ArrayList<>();
        for (int i = 0; i < imgList.size() - 1; i++) {
            ImgItem item = imgList.get(i);
            FeedbackReq.AattachedImg img = new FeedbackReq.AattachedImg();
            img.setName(item.extension);
            byte[] bytes = bitmap2Bytes(item.bitmap);
            String bitmapBytes = Base64.encode(bytes);
            img.setContent(bitmapBytes);
            imgs.add(img);
        }
        feedbackReq.setBase64AttachedImageList(imgs);
        requestEntity.setBody(JsonUtils.toJson(feedbackReq));
        requestEntity.setTimeout(TIME_OUT);
        requestEntity = HttpInjectUtils.injectHttp(FeedBackService.this.getClass(), "uploadFeedbackImgs", requestEntity);
        return requestEntity;
    }*/

    public byte[] bitmap2Bytes(Bitmap bm) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bm.compress(Bitmap.CompressFormat.PNG, 100, baos);
        return baos.toByteArray();
    }

    public static class ImgItem {
        public String extension;
        public Bitmap bitmap;
    }
}
